package me.linczh.bank.service;

import java.util.List;

import me.linczh.bank.bean.EmployeeBean;
import me.linczh.bank.bean.enums.Department;
import me.linczh.bank.bean.enums.Position;
import me.linczh.bank.dao.DaoFactory;
import me.linczh.bank.dao.EmployeeDao;
import me.linczh.framework.query.Query;

public class EmployeeService {
	private EmployeeDao employeeDao = DaoFactory.getEmployeeDao();
	
	public EmployeeBean getEmployee(final String username, String password) {
		List<EmployeeBean> list = employeeDao.query(new Query<EmployeeBean>() {
			String name = username;
			@Override
			public boolean matched(EmployeeBean item) {
				// TODO Auto-generated method stub
				if (item.getUsername().equals(name)) return true;
				else return false;
			}
			@Override
			public String statement() {
				// TODO Auto-generated method stub
				return "select * from employee_tbl where " +
						"username='" + name + "'";
			}
		});
		if (list.size() != 0) {
			if (list.get(0).getPassword().equals(password)) return list.get(0);
		}
		return null;
	}
	
	public List<EmployeeBean> getAllEmployee() {
		return employeeDao.query(new Query<EmployeeBean>() {
			@Override
			public boolean matched(EmployeeBean item) {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			public String statement() {
				// TODO Auto-generated method stub
				return "select * from employee_tbl";
			}
		});
	}
	
	public EmployeeBean createEmployee(final String username, String password,
			Department department, Position position) {
		List<EmployeeBean> list = employeeDao.query(new Query<EmployeeBean>() {
			String params = username;
			@Override
			public boolean matched(EmployeeBean item) {
				// TODO Auto-generated method stub
				if (item.getUsername().equals(params)) return true;
				return false;
			}
			@Override
			public String statement() {
				// TODO Auto-generated method stub
				return "select * from employee_tbl where " +
						"username='" + params + "'";
			}
		});
		if (list.size() != 0) return null;
		EmployeeBean employee = new EmployeeBean();
		employee.setUsername(username);
		employee.setDepartment(department);
		employee.setPassword(password);
		employee.setPosition(position);
		employee = employeeDao.create(employee);
		return employee;
	}
	
	public void deleteEmployee(String employee) {
		EmployeeBean e = employeeDao.findById(employee);
		employeeDao.delete(e);
	}
}
